package com.example.my.fragment;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;


import com.example.my.ExcerciseActivity;
import com.example.my.R;
import com.example.my.bean.Exercise;

import java.util.ArrayList;
import java.util.List;

public class ExcerciseAdapter extends BaseAdapter implements View.OnClickListener{

    private List<Exercise> exerciseList = new ArrayList<>();
    private Context mContext;

    public ExcerciseAdapter(List<Exercise>list,Context ctx){
        exerciseList = list;
        mContext = ctx;
    }
    @Override
    public int getCount()
    {
        return exerciseList == null ? 0: exerciseList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return exerciseList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        List<Object> holder = new ArrayList<>();

        if (convertView == null)
        {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.exercise_item, null);
            TextView idTV=convertView.findViewById(R.id.id);
            TextView titleTV=convertView.findViewById(R.id.title);
            holder.add(idTV);
            holder.add(titleTV);
            holder.add(new Integer(position));
            convertView.setTag(holder);
            convertView.setOnClickListener(this);
        }
        else
        {
            holder = (List<Object>) convertView.getTag();
        }
       ((TextView) holder.get(0)).setText( String.valueOf(position + 1));
        ((TextView)holder.get(1)).setText( exerciseList.get(position).title);
        if (exerciseList.get(position).color == 0){
            ((TextView)holder.get(0)).setBackgroundResource(R.drawable.exercises_bg_1);
        }
        else if (exerciseList.get(position).color == 1)
        {
            ((TextView)holder.get(0)).setBackgroundResource(R.drawable.exercises_bg_2);
        }
        else if (exerciseList.get(position).color == 2)
        {
            ((TextView)holder.get(0)).setBackgroundResource(R.drawable.exercises_bg_3);
        }
        else if (exerciseList.get(position).color == 3)
        {
            ((TextView)holder.get(0)).setBackgroundResource(R.drawable.exercises_bg_4);
        }

        return convertView;
    }

    @Override
    public void onClick(View v) {
        List<Object> holder = (List<Object>) v.getTag();
        int postion = (Integer)holder.get(2);

        Intent intent = new Intent(mContext, ExcerciseActivity.class);
        intent.putExtra("标题",exerciseList.get(postion).title);
        intent.putExtra("链接",exerciseList.get(postion).url);
        mContext.startActivity(intent);

        Toast.makeText(mContext,(postion+1)+"被点击了",Toast.LENGTH_SHORT).show();


    }
}
